package com.qys.livesMall.shop.controller.sys;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.shop.modal.query.ShopStatisticsQuery;
import com.qys.livesMall.shop.modal.entity.ShopDataStatistics;
import com.qys.livesMall.shop.modal.query.QueryShopDataStatistics;
import com.qys.livesMall.shop.modal.vo.ShopDataStatisticsVO;
import com.qys.livesMall.shop.modal.vo.ShopStatisticsInfoVo;
import com.qys.livesMall.shop.service.H2ShopStatisticsInfoService;
import com.qys.livesMall.shop.service.ShopDataStatisticsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * 店铺流量消耗统计表(ShopDataStatistics)表控制层
 *
 * @author makejava
 * @since 2023-07-10 09:47:13
 */
@RestController
@RequestMapping("/sys/shopDataStatistics")
@RequiredArgsConstructor
@Tag(name = "sys-店铺流量剩余统计", description = "sys-店铺流量剩余统计")
public class SysShopDataStatisticsController {

    private final ShopDataStatisticsService shopDataStatisticsService;

    private final H2ShopStatisticsInfoService h2ShopStatisticsInfoService;

    /**
     * 分页查询所有数据
     *
     * @return 所有数据
     */
    @PostMapping("/selectAll")
    @Operation(summary = "分页查询所有数据")
    public R<IPage<ShopDataStatisticsVO>> selectAll(@RequestBody QueryShopDataStatistics query) {
        Wrapper<ShopDataStatistics> queryWrapper = Wrappers.<ShopDataStatistics>lambdaQuery().like(StringUtils.isNotBlank(query.getShopName()), ShopDataStatistics::getShopName, query.getShopName())
                .eq(null != query.getStatus(), ShopDataStatistics::getStatus, query.getStatus()).eq(ShopDataStatistics::getIsDelete,0);
        IPage<ShopDataStatisticsVO> page = shopDataStatisticsService.page(new Page<>(query.getPageNum(), query.getPageSize()), queryWrapper).convert(e -> {
            ShopDataStatisticsVO statisticsVO = new ShopDataStatisticsVO();
            BeanUtils.copyProperties(e, statisticsVO);
            return statisticsVO;
        });
        return R.ok(page);
    }

    /**
     * 修改余量
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/editResidual/{id}/{val}")
    @Operation(summary = "修改余量")
    public R<?> editResidual(@PathVariable Serializable id, @PathVariable Integer val) {
        ShopDataStatistics statistics = shopDataStatisticsService.getById(id);
        if(null==statistics){
            return R.fail();
        }
        ShopDataStatistics newStatistics = new ShopDataStatistics();
        newStatistics.setId(statistics.getId());
        newStatistics.setResidue(val);
        shopDataStatisticsService.updateById(newStatistics);
        return R.ok();
    }


    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @PostMapping("/delete")
    @Operation(summary = "删除数据")
    public R<?> delete(@RequestBody List<Long> idList) {
        shopDataStatisticsService.removeByIds(idList);
        return R.ok();
    }


    @PostMapping("shop/page")
    @Operation(summary = "分页查询用户统计信息")
    public R<Page<ShopStatisticsInfoVo>> shopInfoPage(@RequestBody ShopStatisticsQuery shopStatisticsQuery) {
        return R.ok(h2ShopStatisticsInfoService.listShopStatisticsInfo(shopStatisticsQuery));
    }
}

